package com.xingda.packing.dmds.common;

import java.math.BigDecimal;
import java.util.Objects;

public class ScanCodeUtils {

    public static final BigDecimal green_min = new BigDecimal("60");
    public static final BigDecimal green_max = new BigDecimal("90");
    public static final BigDecimal red_min = new BigDecimal("1");
    public static final BigDecimal red_min1 = new BigDecimal("175");
    public static final BigDecimal red_max = new BigDecimal("20");
    public static final BigDecimal red_max1 = new BigDecimal("180");
    public static final BigDecimal blue_min = new BigDecimal("100");
    public static final BigDecimal blue_max = new BigDecimal("120");
    public static final BigDecimal pink_min = new BigDecimal("150");
    public static final BigDecimal pink_min1 = new BigDecimal("181");
    public static final BigDecimal pink_max = new BigDecimal("174");
    public static final BigDecimal pink_max1 = new BigDecimal("190");

    public static String scanCodeToBarCodeAndColor2(String scanCode) {
        char[] chars = scanCode.toCharArray();
        String substring = "##################_Z";
        for (int i = 0; i < chars.length; i++) {
            if (String.valueOf(chars[i]).equals("X")) {
                substring = scanCode.substring(i);
            }
        }
        if (substring.startsWith("XC")) {
            substring = substring.substring(0, 13);
        } else if (substring.startsWith("XD")) {
            substring = substring.substring(0, 16);
        }
        String code = scanCode.replaceAll(" ", "");
        String[] split = code.split(",");
        for (int i = 0; i < split.length; i++) {
            if (split[i].equals("0&0004")) {
                code = split[i + 1];
                break;
            }
        }
        return substring + "_" + getColor(new BigDecimal(code.split("&")[0]));
    }

    private static String getColor(BigDecimal value) {
        if (value.compareTo(green_min) >= 0 && value.compareTo(green_max) <= 0)
            return "B";
        if ((value.compareTo(red_min) >= 0 && value.compareTo(red_max) <= 0) || (value.compareTo(red_min1) >= 0 && value.compareTo(red_max1) <= 0))
            return "A";
        if (value.compareTo(blue_min) >= 0 && value.compareTo(blue_max) <= 0)
            return "C";
        if ((value.compareTo(pink_min) >= 0 && value.compareTo(pink_max) <= 0) || (value.compareTo(pink_min1) >= 0 && value.compareTo(pink_max1) <= 0))
            return "D";
        return "Z";
    }


    public static String getColor(String vFree2, String color) {
        if (Objects.equals(vFree2, "B80") && color.equals("D")) {
            //B80识别为粉色的 改判为红
            return "A";
        }
        if (Objects.equals(vFree2, "B40") && color.equals("A")) {
            //B40识别为红色的 改判为粉
            return "D";
        }
        if (Objects.equals(vFree2, "B80/17") && color.equals("A")) {
            //B80/17识别为红色的 改判为粉
            return "D";
        }
        if (Objects.equals(vFree2, "B60") && color.equals("D")) {
            //B60识别为粉色的 改判为红
            return "A";
        }
        return color;
    }



    public static void main(String[] args) {
        String code = scanCodeToBarCodeAndColor2("000110#0117@0094&0006,1,7,0&0018,XDB43E1112009326 &0006,1,7,0&0004,59.&0006,1,7,0&0024,3172.,499.2,0.000,0.000;0000#");
        System.out.println(code);
    }

}
